package cz.boxdesign.library.common.dao;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cz.boxdesign.library.configuration.DatabaseOperation;
import cz.boxdesign.library.entity.Publisher;

@Component("commonObjectDao")
public class CommonObjectDaoImpl implements CommonObjectDao {

	protected final Log log = LogFactory.getLog(CommonObjectDaoImpl.class);

	@Autowired
	DatabaseOperation jdbc;

	@Autowired
	CommonDao commonDao;

	@Override
	public boolean isAccountExpired(Integer id) throws DatabaseOperationException {
		try {
			jdbc.beginTransaction();
			CallableStatement cs = jdbc.getCallableStatement("{? = call isAccountExpired(?)}");
			cs.registerOutParameter(1, Types.INTEGER);
			cs.setInt(2, id);
			cs.execute();
			boolean ret = cs.getBoolean(1);
			cs.close();
			jdbc.endTransaction();
			return ret;
		} catch (SQLException e) {
			jdbc.rollback();
			jdbc.reconect();
			log.info("RECONNECT");
			throw new DatabaseOperationException("Database Error !\n" + e.getMessage());
		}
	}

	@Override
	public void prolongAccount(Integer id) throws DatabaseOperationException {
		try {
			jdbc.beginTransaction();
			CallableStatement cs = jdbc.getCallableStatement("{call prolongAccount(?)}");
			cs.registerOutParameter(1, Types.INTEGER);
			cs.setInt(1, id);
			cs.execute();
			cs.close();
			jdbc.endTransaction();
		} catch (SQLException e) {
			jdbc.rollback();
			jdbc.reconect();
			log.info("RECONNECT");
			e.printStackTrace();
			throw new DatabaseOperationException("Database Error !\n" + e.getMessage());
		}
	}

	@Override
	public boolean isLoginAvailabel(String login) throws DatabaseOperationException {
		try {
			jdbc.beginTransaction();
			CallableStatement cs = jdbc.getCallableStatement("{? = call isLoginAvailabe(?)}");
			cs.registerOutParameter(1, Types.INTEGER);
			cs.setString(2, login);
			cs.execute();
			boolean ret = cs.getBoolean(1);
			cs.close();
			jdbc.endTransaction();
			return ret;
		} catch (SQLException e) {
			jdbc.rollback();
			jdbc.reconect();
			log.info("RECONNECT");
			throw new DatabaseOperationException("Database Error !\n" + e.getMessage());
		}
	}

	@Override
	public void borrowReservedBook(Integer user, Integer book) {
		try {
			jdbc.beginTransaction();
			CallableStatement cs = jdbc.getCallableStatement("{call REZERVACETOPUJCENO(?,?)}");
			cs.registerOutParameter(1, Types.INTEGER);
			cs.setInt(1, user);
			cs.setInt(2, book);
			cs.execute();
			cs.close();
			jdbc.endTransaction();
		} catch (SQLException e) {
			jdbc.rollback();
			jdbc.reconect();
			log.info("RECONNECT");
			e.printStackTrace();
			throw new DatabaseOperationException("Database Error !\n" + e.getMessage());
		}
	}

	@Override
	public boolean isPublisherNameAvailable(String name) {
		List<Publisher> list = commonDao.getDao(Publisher.class).getByClause("and NAZEV_N like '" + name + "'");
		if (null == list || list.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

}
